// 引入模块
const express = require('express');

const bodyParser = require('body-parser');

const multer  = require('multer');
// 引入路由
const userRouter = require('./routers/user.js');

const carouseRouter = require('./routers/carouselMap.js');

const laptopRouter = require('./routers/laptop.js');

const orderRouter=require('./routers/order.js');

const orderDetails=require('./routers/orderDetails.js');

const imgRouter=require('./routers/imgRouter.js');

const app = express();
app.listen(8080);


app.use(express.static("views"));
app.use(bodyParser.urlencoded({
  extended: false
}));

/* 解决跨域问题 */
app.all("*", function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With");
  res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
  res.header("X-Powered-By",'3.2.1');
  if(req.method=="OPTIONS") res.send(200);
  else next();
  });
  
//2 实例化  
let objMulter = multer({ dest: './views/imgStoreHouse/' });
app.use(objMulter.any()); 

app.use('/v1/users', userRouter);
app.use('/v1/maps', carouseRouter);
app.use('/v1/laptops', laptopRouter);
app.use('/v1/orders',orderRouter);
app.use('/v1/orDetails',orderDetails);
app.use('/v1/imgRouter',imgRouter);

app.use((err, req, res, next) => {
  //err 接收的错误
  console.log(err);
  res.send({ code: 500, msg: '服务器端错误' });
});
